/*
 ============================================================================
 Name		: MyS60AlarmAppView.cpp
 Author	  : 
 Copyright   : Your copyright notice
 Description : Application view implementation
 ============================================================================
 */

// INCLUDE FILES
#include <coemain.h>
#include <barsread.h>	//for resource reader 
#include <MyS60Alarm.rsg>

#include "MyS60AlarmAppView.h"
#include "AlarmObject.h"
#include "MyS60AlarmAppUi.h"

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CMyS60AlarmAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CMyS60AlarmAppView* CMyS60AlarmAppView::NewL(const TRect& aRect)
	{
	CMyS60AlarmAppView* self = CMyS60AlarmAppView::NewLC(aRect);
	CleanupStack::Pop(self);
	return self;
	}

// -----------------------------------------------------------------------------
// CMyS60AlarmAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CMyS60AlarmAppView* CMyS60AlarmAppView::NewLC(const TRect& aRect)
	{
	CMyS60AlarmAppView* self = new ( ELeave ) CMyS60AlarmAppView;
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

// -----------------------------------------------------------------------------
// CMyS60AlarmAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CMyS60AlarmAppView::ConstructL(const TRect& aRect)
	{
	// Create a window for this application view
	CreateWindowL();

    CreateAlarmList();
    LoadAlarms();	
	
	// Set the windows size
	SetRect(aRect);

	// Activate the window, which makes it ready to be drawn
	ActivateL();
	}

// -----------------------------------------------------------------------------
// CMyS60AlarmAppView::CMyS60AlarmAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CMyS60AlarmAppView::CMyS60AlarmAppView()
	{
	// No implementation required
	}

// -----------------------------------------------------------------------------
// CMyS60AlarmAppView::~CMyS60AlarmAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CMyS60AlarmAppView::~CMyS60AlarmAppView()
	{
	if(iAlarmList)
		{
		delete iAlarmList;
	    }
	}

// -----------------------------------------------------------------------------
// CMyS60AlarmAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CMyS60AlarmAppView::Draw(const TRect& /*aRect*/) const
	{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc();

	// Gets the control's extent
	TRect drawRect(Rect());

	// Clears the screen
	gc.Clear(drawRect);

	}

// -----------------------------------------------------------------------------
// CMyS60AlarmAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CMyS60AlarmAppView::SizeChanged()
	{
	//DrawNow();
	iAlarmList->SetRect(Rect());
	}

void CMyS60AlarmAppView::CreateAlarmList()
	{
	iAlarmList = new (ELeave) CAknDoubleTimeStyleListBox;
    iAlarmList->SetContainerWindowL(*this);
    
    TResourceReader reader;
    iEikonEnv->CreateResourceReaderLC(reader, R_ALARM_LIST);
    iAlarmList->ConstructFromResourceL(reader);
    CleanupStack::PopAndDestroy();
    
    iAlarmList->SetListBoxObserver(this);
    
    iAlarmList->CreateScrollBarFrameL(ETrue);
    iAlarmList->ScrollBarFrame()->SetScrollBarVisibilityL(
    		CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
	}

void CMyS60AlarmAppView::LoadAlarms()
	{
    iAlarmListBoxModel = iAlarmList->Model();
    iAlarmListBoxModel->SetOwnershipType(ELbmOwnsItemArray);
    
    CDesCArray* alarmArray = STATIC_CAST(CDesCArray*, 
    		iAlarmListBoxModel->ItemTextArray());
   
    CMyS60AlarmAppUi* appUi = (CMyS60AlarmAppUi*)iEikonEnv->AppUi();
    RASCliSession ras = appUi->AlarmServerSession();
    
    ras.Connect();
    
    RArray<TAlarmId> alarmIdArray;
    alarmIdArray.Append(32);
    TRAPD(result, ras.GetAlarmIdListL(alarmIdArray))
    
    for (TInt val = 0; val <= alarmIdArray.Count(); val++)
    	{
    	
    	TAlarmId id = alarmIdArray[val];
    	TASShdAlarm alarm;
    	TInt err = ras.GetAlarmDetails(id, alarm);
    	
    	CAlarmObject* alarmObject = CAlarmObject::NewL(&alarm);
    	
    	//TDesC* alarmText = alarmObject->NextDueTimeL();
    	//TBuf<64> alarmText = (TBuf<64>)alarmObject->NextDueTimeL();
        _LIT(alarmTextTemp, "13:00\tPM\tAlarm A\tthe first alarm\tmusic");
        const TDesC16* alarmText = &alarmTextTemp;
        alarmArray->AppendL(*alarmText);
    	//alarmArray->AppendL((*alarmObject->NextDueTimeL()));
    	}
    
    /*
     * format of the alarmInfo text:
     * <time><\t><pm or am><\t><title><\t><message><\t><other>
     * 
     * */
    	
    _LIT(alarmBTextB, "Alarm B");
    alarmArray->AppendL(alarmBTextB);
    
    _LIT(alarmCTextC, "\tAlarm C");
    alarmArray->AppendL(alarmCTextC);
    
    alarmIdArray.Close();
	}

// ---------------------------------------------------------
// CMyViewContainer::HandleControlEventL(
//     CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CMyS60AlarmAppView::HandleListBoxEventL(CEikListBox* /*aListBox*/, 
		TListBoxEvent aListBoxEvent)
    {
    
    if (aListBoxEvent == MEikListBoxObserver::EEventEnterKeyPressed)
		{
		TInt currentItemIndex = iAlarmList->CurrentItemIndex();
		
        CMyS60AlarmForm* alarmEditor;
		alarmEditor = new (ELeave) CMyS60AlarmForm(iAlarmList);
		alarmEditor->SetMopParent( this );
		alarmEditor->ExecuteLD( R_ALARMFORM );
		}
    }

TKeyResponse CMyS60AlarmAppView::OfferKeyEventL(
		const TKeyEvent& aKeyEvent, TEventCode aType)
	{
	if ( ( iAlarmList ) && ( aType == EEventKey ) )	
		{
		return iAlarmList->OfferKeyEventL(aKeyEvent, aType);
		}
	else
		{
		return EKeyWasNotConsumed;
		}
	}

// ---------------------------------------------------------
// CMyViewContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CMyS60AlarmAppView::CountComponentControls() const
    {
    return 1; // return nbr of controls inside this container
    }

// ---------------------------------------------------------
// CMyViewContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CMyS60AlarmAppView::ComponentControl(TInt aIndex) const
    {
    switch ( aIndex )
        {
        case 0:
        	return iAlarmList;
        default:
            return NULL;
        }
    }

// End of File
